package com.fancy.learncenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.YiGouTongAdapter;
import com.fancy.learncenter.bean.BannerListDataBean;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.YgtDataBean;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.view.PopupWindowPayYgt;
import com.fancy.learncenter.view.PopupWindowYgtCopy;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class YiGouTongActivity extends BaseActivity {
    PopupWindowPayYgt popupWindowPayYgt;
    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.top_recycleView)
    RecyclerView topRecycleView;


    PopupWindowYgtCopy popupWindowYgtCopy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yi_gou_tong, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);

        initToolbar("易沟通");
        setRight("同步模板", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (TextUtils.isEmpty(ygt_status)) {
                    Intent intent = new Intent(YiGouTongActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                    return;
                }

                if (ygt_status.equals("1")) {

                    if (popupWindowYgtCopy == null) {
                        popupWindowYgtCopy = new PopupWindowYgtCopy(YiGouTongActivity.this, recycleView);
                    }
                    popupWindowYgtCopy.isShow();

                } else if (ygt_status.equals("3")) {
                    if (popupWindowPayYgt == null) {
                        popupWindowPayYgt = new PopupWindowPayYgt(YiGouTongActivity.this, recycleView);
                        popupWindowPayYgt.setFreeCallBack(new PopupWindowPayYgt.FreeCallBack() {
                            @Override
                            public void freeCallBack() {
                                ygtFrer();
                            }
                        });
                    }

                    popupWindowPayYgt.setFreeHide();
                    popupWindowPayYgt.isShow();
                } else if (ygt_status.equals("0") || ygt_status.equals("2")) {
                    if (popupWindowPayYgt == null) {
                        popupWindowPayYgt = new PopupWindowPayYgt(YiGouTongActivity.this, recycleView);
                        popupWindowPayYgt.setFreeCallBack(new PopupWindowPayYgt.FreeCallBack() {
                            @Override
                            public void freeCallBack() {
                                ygtFrer();
                            }
                        });
                    }

                    popupWindowPayYgt.isShow();
                }


            }
        });

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        recycleView.setLayoutManager(gridLayoutManager);

        ArrayList itemDatas = new ArrayList<>();

        adapter = new

                YiGouTongAdapter(this, itemDatas);
        recycleView.setAdapter(adapter);
        adapter.setOnClickCallBack(new YiGouTongAdapter.OnClickCallBack() {
            @Override
            public void onClick(YgtDataBean.ListBean item) {

                if (TextUtils.isEmpty(ygt_status)) {
                    Intent intent = new Intent(YiGouTongActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                    return;
                }

                if (ygt_status.equals("2")) {
                    if (item.getClassify_name().equals("根管治疗")) {
                        Intent intent = new Intent(YiGouTongActivity.this, YgtDetailActivity.class);
                        intent.putExtra("classifyId", item.getId());
                        startActivity(intent);

                    } else {
                        if (popupWindowPayYgt == null) {
                            popupWindowPayYgt = new PopupWindowPayYgt(YiGouTongActivity.this, recycleView);
                            popupWindowPayYgt.setFreeCallBack(new PopupWindowPayYgt.FreeCallBack() {
                                @Override
                                public void freeCallBack() {
                                    Intent intent = new Intent(YiGouTongActivity.this, YgtDetailActivity.class);
                                    intent.putExtra("classifyId", adapter.freeId);
                                    startActivity(intent);
                                }
                            });
                        }
                        popupWindowPayYgt.isShow();
                    }

                } else if (ygt_status.equals("1")) {
                    Intent intent = new Intent(YiGouTongActivity.this, YgtDetailActivity.class);
                    intent.putExtra("classifyId", item.getId());
                    startActivity(intent);

                } else if (ygt_status.equals("3")) {
                    if (popupWindowPayYgt == null) {
                        popupWindowPayYgt = new PopupWindowPayYgt(YiGouTongActivity.this, recycleView);
                        popupWindowPayYgt.setFreeCallBack(new PopupWindowPayYgt.FreeCallBack() {
                            @Override
                            public void freeCallBack() {
                                ygtFrer();
                            }
                        });
                    }

                    popupWindowPayYgt.setFreeHide();
                    popupWindowPayYgt.isShow();
                } else if (ygt_status.equals("0")) {
                    if (popupWindowPayYgt == null) {
                        popupWindowPayYgt = new PopupWindowPayYgt(YiGouTongActivity.this, recycleView);
                        popupWindowPayYgt.setFreeCallBack(new PopupWindowPayYgt.FreeCallBack() {
                            @Override
                            public void freeCallBack() {
                                ygtFrer();
                            }
                        });
                    }

                    popupWindowPayYgt.isShow();
                }
            }
        });

        initTop();

        bannerList();

    }

    YiGouTongAdapter topAdapter;

    private void initTop() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        topRecycleView.setLayoutManager(gridLayoutManager);
        topAdapter = new YiGouTongAdapter(this, new ArrayList<YgtDataBean.ListBean>());
        topRecycleView.setAdapter(topAdapter);
        topAdapter.setOnClickCallBack(new YiGouTongAdapter.OnClickCallBack() {
            @Override
            public void onClick(YgtDataBean.ListBean item) {

                if (TextUtils.isEmpty(ygt_status)) {
                    Intent intent = new Intent(YiGouTongActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                    return;
                }

                if (ygt_status.equals("2")) {
                    if (item.getClassify_name().equals("根管治疗")) {
                        Intent intent = new Intent(YiGouTongActivity.this, YgtDetailActivity.class);
                        intent.putExtra("classifyId", item.getId());
                        startActivity(intent);

                    } else {
                        if (popupWindowPayYgt == null) {
                            popupWindowPayYgt = new PopupWindowPayYgt(YiGouTongActivity.this, recycleView);
                            popupWindowPayYgt.setFreeCallBack(new PopupWindowPayYgt.FreeCallBack() {
                                @Override
                                public void freeCallBack() {
                                    Intent intent = new Intent(YiGouTongActivity.this, YgtDetailActivity.class);
                                    intent.putExtra("classifyId", adapter.freeId);
                                    startActivity(intent);
                                }
                            });
                        }
                        popupWindowPayYgt.isShow();
                    }

                } else if (ygt_status.equals("1")) {
                    Intent intent = new Intent(YiGouTongActivity.this, YgtDetailActivity.class);
                    intent.putExtra("classifyId", item.getId());
                    startActivity(intent);

                } else if (ygt_status.equals("3")) {
                    if (popupWindowPayYgt == null) {
                        popupWindowPayYgt = new PopupWindowPayYgt(YiGouTongActivity.this, recycleView);
                        popupWindowPayYgt.setFreeCallBack(new PopupWindowPayYgt.FreeCallBack() {
                            @Override
                            public void freeCallBack() {
                                ygtFrer();
                            }
                        });
                    }

                    popupWindowPayYgt.setFreeHide();
                    popupWindowPayYgt.isShow();
                } else if (ygt_status.equals("0")) {
                    if (popupWindowPayYgt == null) {
                        popupWindowPayYgt = new PopupWindowPayYgt(YiGouTongActivity.this, recycleView);
                        popupWindowPayYgt.setFreeCallBack(new PopupWindowPayYgt.FreeCallBack() {
                            @Override
                            public void freeCallBack() {
                                ygtFrer();
                            }
                        });
                    }

                    popupWindowPayYgt.isShow();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getDatas();
    }

    YiGouTongAdapter adapter;

    //易沟通账号状态  0:没有 1:有效 2:试用 3:过期
    String ygt_status = "0";

    private void getDatas() {
        Map map = new HashMap();

        HttpMehtod.getInstance().ygtList(map, new IdeaObserver<BaseDataBean<YgtDataBean>>() {

            @Override
            public void onSuccess(BaseDataBean<YgtDataBean> ygtDataBeanBaseDataBean) {
                if (ygtDataBeanBaseDataBean.getData() != null) {

                    ArrayList<YgtDataBean.ListBean> topData = new ArrayList<>();
                    ArrayList<YgtDataBean.ListBean> itemData = new ArrayList<>();

                    if (ygtDataBeanBaseDataBean.getData().getList().size() > 6) {

                        for (int i = 0; i < ygtDataBeanBaseDataBean.getData().getList().size(); i++) {
                            if (i < 6) {
                                topData.add(ygtDataBeanBaseDataBean.getData().getList().get(i));
                            } else {
                                itemData.add(ygtDataBeanBaseDataBean.getData().getList().get(i));
                            }

                        }
                        topAdapter.notifyDataSetChanged(topData);
                        adapter.notifyDataSetChanged(itemData);
                    } else {
                        topAdapter.notifyDataSetChanged(ygtDataBeanBaseDataBean.getData().getList());
                    }


                    ygt_status = ygtDataBeanBaseDataBean.getData().getYgt_status();
                }

            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }

    private void ygtFrer() {
        Map map = new HashMap();
        HttpMehtod.getInstance().ygtFreeDetailList(map, new IdeaObserver<BaseDataBean>() {

            @Override
            public void onSuccess(BaseDataBean ygtDataBeanBaseDataBean) {
                Intent intent = new Intent(YiGouTongActivity.this, YgtDetailActivity.class);
                intent.putExtra("classifyId", adapter.freeId);
                startActivity(intent);
            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }

    private void bannerList() {
        Map map = new HashMap();
        map.put("site", "4");

        HttpMehtod.getInstance().bannerList(map, new IdeaObserver<BaseDataBean<ArrayList<BannerListDataBean>>>() {

            @Override
            public void onSuccess(final BaseDataBean<ArrayList<BannerListDataBean>> arrayListBaseDataBean) {

//                if (arrayListBaseDataBean.getData() != null && arrayListBaseDataBean.getData().size() > 0) {
//                    adSimdraweeview.setImageURI(arrayListBaseDataBean.getData().get(0).getImgsrc());
//                    adSimdraweeview.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//
//                            if (!TextUtils.isEmpty(arrayListBaseDataBean.getData().get(0).getLink())) {
//                                Intent intent = new Intent(YiGouTongActivity.this, H5Activity.class);
//                                intent.putExtra(H5Activity.URL_KEY, arrayListBaseDataBean.getData().get(0).getLink());
//                                startActivity(intent);
//                            }
//
//                        }
//                    });
//                } else {
//                    adSimdraweeview.setVisibility(View.GONE);
//                }

            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }
}
